package gitee.cheungming.modules.dmp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import gitee.cheungming.common.service.impl.CrudServiceImpl;
import gitee.cheungming.common.utils.ConvertUtils;
import gitee.cheungming.modules.dmp.dao.VehicleDao;
import gitee.cheungming.modules.dmp.dto.VehicleDTO;
import gitee.cheungming.modules.dmp.entity.VehicleEntity;
import gitee.cheungming.modules.dmp.service.VehicleService;
import cn.hutool.core.util.StrUtil;
import gitee.cheungming.modules.security.user.SecurityUser;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 车辆
 *
 * @author cheungming cheungming@gmail.com
 * @since 1.0.0 2025-08-21
 */
@Service
public class VehicleServiceImpl extends CrudServiceImpl<VehicleDao, VehicleEntity, VehicleDTO> implements VehicleService {

    @Override
    public QueryWrapper<VehicleEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");
        String plateNumber = (String) params.get("plateNumber");
        String vin = (String) params.get("vin");
        String type = (String) params.get("type");
        String brand = (String) params.get("brand");

        QueryWrapper<VehicleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);
        wrapper.like(StrUtil.isNotBlank(plateNumber), "plate_number", plateNumber);
        wrapper.like(StrUtil.isNotBlank(vin), "vin", vin);
        wrapper.like(StrUtil.isNotBlank(type), "type", type);
        wrapper.like(StrUtil.isNotBlank(brand), "brand", brand);

        return wrapper;
    }


    @Override
    public void save(VehicleDTO dto) {
        VehicleEntity vehicleEntity = ConvertUtils.sourceToTarget(dto, VehicleEntity.class);
        vehicleEntity.setCreator(SecurityUser.getUserId());
        vehicleEntity.setCreateDate(new Date());
        insert(vehicleEntity);
    }


    @Override
    public void update(VehicleDTO dto) {
        VehicleEntity vehicleEntity = ConvertUtils.sourceToTarget(dto, VehicleEntity.class);
        vehicleEntity.setUpdater(SecurityUser.getUserId());
        vehicleEntity.setUpdateDate(new Date());
        updateById(vehicleEntity);
    }
}